﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Buyer
{
    public partial class mfApplyDrawals : msBsc.Cls.mbPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void InitBaseInfo()
        {
            if (!msBsc.SvRef.UserInfo.Online)
            {
                this.ExecJScript(@"window.location.href=""/mp_UserLogin.htm""");
                return;
            }

            

            System.Data.DataSet ds = null;
            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                ds = objCBizCustomerBaseInfo.GetAliPayAndBank();
            }

            if (ds == null || ds.Tables[0].Rows.Count <= 0)
            {
                this.ExecJScript(@"window.location.href=""/mp_UserLogin.htm""");
                return;
            }

            if (ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"] == DBNull.Value || string.IsNullOrEmpty(ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"].ToString()))
            //&& ds.Tables[0].Rows[0]["CUSTOMER_BANK_ACCOUNT"] == DBNull.Value)
            {
                this.ExecJScript(@"alert(""当前账户未绑定账户信息，请绑定"");window.location.href=""/mb_BindBalanceinfo.htm""");
                return;
            }
            if (!msBsc.SvRef.UserInfo.UsrIsCmfInnerPwd)
            {
                this.ExecJScript(@"window.location.href=""/mb_CmfInnerPayPwd.htm?ReturnUrl=/mb_ApplyDrawals.htm""");
                return;
            }
            string execJsJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", 200, "");
            this.ExecJScript(@"SetAccountInfo(" + execJsJson + ")");
        }

        public override void DoAction()
        {
            if (this.strAction == "SubmitApply")
            {
                ComfirmApply();
            }

            if (this.strAction == "InitInfo")
            {
                InitBaseInfo();
            }
        }

        /// <summary>
        /// 提现操作
        /// </summary>
        private void ComfirmApply()
        {
            #region 验证登录信息
            if (!msBsc.SvRef.UserInfo.Online)
            {
                this.ExecJScript("window.location.href='/mp_UserLogin.htm'");
                return;
            }

            if (!msBsc.SvRef.UserInfo.UsrIsCmfInnerPwd)
            {
                this.ExecJScript("window.location.href='/mb_CmfInnerPayPwd.htm?ReturnUrl=/mb_ApplyDrawals.htm'");
                return;
            }
            #endregion

            string balance = this.GetParamHtValue("Balance");
            string drawalsType = this.GetParamHtValue("DrawalsType");

            #region 验证输入信息
            decimal dbalance = 0;
            try
            {
                dbalance = Convert.ToDecimal(balance);
            }
            catch (Exception)
            {
                throw new Exception("提现金额格式错误，请重新输入");
                return;
            }

            if (dbalance <= 0)
            {
                this.Alert("提现金额必须大于零");
                return;
            }
            #endregion

            System.Data.DataSet ds = null;

            #region 验证账户信息
            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                ds = objCBizCustomerBaseInfo.GetAliPayAndBank();
            }

            if (Convert.ToDecimal(ds.Tables[0].Rows[0]["CUSTOMER_DRAWALS_ACCOUNT"]) <= 0)
            {
                this.Alert("当前账户没有可提现金额");
                return;
            }

            if (drawalsType == "1"
                && (ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"] == DBNull.Value
                     || string.IsNullOrEmpty(ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"].ToString())))
            {
                this.Alert("当前账户暂未绑定支付宝账户，不能通过支付宝账户进行提现");
                return;
            }

            if (drawalsType == "2"
                && (ds.Tables[0].Rows[0]["CUSTOMER_BANK_ACCOUNT"] == DBNull.Value
                    || string.IsNullOrEmpty(ds.Tables[0].Rows[0]["CUSTOMER_BANK_ACCOUNT"].ToString())))
            {
                this.Alert("当前账户暂未绑定银行账户，不能通过银行账户进行提现");
                return;
            }

            if (Convert.ToDecimal(ds.Tables[0].Rows[0]["CUSTOMER_DRAWALS_ACCOUNT"]) - dbalance < 0)
            {
                this.Alert("当前账户余额不足");
                return;
            }
            #endregion

            string applyResult = string.Empty;

            #region 进行提现操作
            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                applyResult = objCBizCustomerBaseInfo.ApplyDrawals(dbalance, drawalsType);
            }

            if (applyResult != "OK")
            {
                this.Alert(applyResult);
                return;
            }

            string drawalsAccount = drawalsType == "1" ? "支付宝账户：" + ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"].ToString() : ds.Tables[0].Rows[0]["BANK_NME"] + "：" + ds.Tables[0].Rows[0]["CUSTOMER_BANK_ACCOUNT"].ToString();

            string noticeMsg = @"提现成功，提现金额为：\r\n ￥" + balance + @"\r\n提现账户为：\r\n" + drawalsAccount + @"\r\n提现资金将于3个工作日内，转入您的账户，请注意查收";
            this.ExecJScript(@"ShowSuccessMsg('" + noticeMsg + "')");
            #endregion

        }
    }
}